package CustomUI;

import java.util.Iterator;

import org.Constants;
import org.Global;
import org.TempVariables;
import org.ydClipBoardManager;
import org.json.JSONException;
import org.json.JSONObject;

import com.ydev.lazywallet.R;
import com.ydev.lazywallet.gui.EnterKey;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ViewCellCell extends FrameLayout {
	
	private Activity mActivity;
	private Context mContext;
	private LayoutInflater layoutInflater;
	
	LinearLayout llFrame;
	TextView tvKey;
	TextView tvValue;
	Button btnCopyAll;
	
	public ViewCellCell(Activity activity, Context context) {
		super(context);
		this.mContext = context;
		this.mActivity=activity;
		inflate();
		bindViews();
		init();
	}

	private void inflate() {
		layoutInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.view_cell_cell, this, true);
	}

	private void bindViews() {
		/*llMain = (LinearLayout) findViewById(R.id.llMain);
		imgPicture = (ImageView) findViewById(R.id.imgSmallPicture);
		bar = (View) findViewById(R.id.view1);
		imgcheck = (ImageView) findViewById(R.id.imgCheckbox);*/
	}
	
	public void init(){
		llFrame=(LinearLayout)findViewById(R.id.llFrame);
		tvKey=(TextView)findViewById(R.id.tv_key);
		tvValue=(TextView)findViewById(R.id.tv_value);
		btnCopyAll=(Button)findViewById(R.id.btncopy);
	}
	
	public void setTitle(final String s){
		tvKey.setText(s);
		
		btnCopyAll.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try {
				JSONObject data=new JSONObject(Global.sampleData);
				JSONObject d=data.optJSONObject(s);
				
				Iterator<String> iter = d.keys();
				
				String s="";
			    while (iter.hasNext()) {
			        String key = iter.next();
			        try {
			            Object value = d.get(key);
			            s=s+value.toString() + ",";
			        } catch (JSONException e) {
			            // Something went wrong!
			        }
			    }
			    
			    ydClipBoardManager.insertIntoClipBoard(s);
				
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		
		llFrame.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				TempVariables.SelectedHomeCell=s;
				Intent i = new Intent(mActivity, EnterKey.class);
				mActivity.startActivity(i);
			}
		});
		
	}

	public void setKeyValue(String string, final String string2) {
		tvKey.setText(string);
		tvValue.setText(string2);
		
		btnCopyAll.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 ydClipBoardManager.insertIntoClipBoard(string2);
				
			}
		});
		
	}

}
